$(document).ready(function () {
    toastr.options = {
        closeButton: true,
        debug: false,
        progressBar: false,
        positionClass: "toast-top-right",
        onclick: null,
        showDuration: "1000",
        hideDuration: "1000",
        timeOut: "2000",
        extendedTimeOut: "1000",
        showEasing: "swing",
        hideEasing: "linear",
        showMethod: "fadeIn",
        hideMethod: "fadeOut"
    };
    let QRCodeButton = $("#QRCodeButton");
    let codeImg = $("#QRCode");
    let textArea = $("#content");
    let reset = $("#reset");
    codeImg.attr("src", "QRCode?content=" + encodeURIComponent(textArea.val()));
    textArea.keyup(function () {
        codeImg.attr("src", "QRCode?content=" + encodeURIComponent(textArea.val()))
    });
    QRCodeButton.click(function () {
        codeImg.attr("src", "QRCode?content=" + encodeURIComponent(textArea.val()))
    });

    let year1 = $("#year1");
    let month1 = $("#month1");
    let day1 = $("#day1");
    let diffDay = $("#diffDay");

    let year2 = $("#year2");
    let month2 = $("#month2");
    let day2 = $("#day2");

    let year3 = $("#year3");
    let month3 = $("#month3");
    let day3 = $("#day3");

    year1.val(new Date().getFullYear());
    month1.val(new Date().getMonth() + 1);
    day1.val(new Date().getDate());

    year2.val(new Date().getFullYear());
    month2.val(new Date().getMonth() + 1);
    day2.val(new Date().getDate());

    diffDay.keyup(function (event) {
        if (event.keyCode === 13) {
            $.ajax({
                type: "POST",
                url: "date/computeDate",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(
                    {
                        "date1": year1.val() + "-" + month1.val() + "-" + day1.val(),
                        "differenceDate": diffDay.val()
                    }
                ),
                dataType: "json",
                success: function (data) {
                    let split = data.date.split("-");
                    $("#computeDateSpan").html(split[0] + "年" + split[1] + "月" + split[2] + "日");
                }
            });
        }
    });

    day3.keyup(function (event) {
        if (event.keyCode === 13) {
            $.ajax({
                type: "POST",
                url: "date/differenceDate",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(
                    {
                        "date2": year2.val() + "-" + month2.val() + "-" + day2.val(),
                        "date3": year3.val() + "-" + month3.val() + "-" + day3.val()
                    }
                ),
                dataType: "json",
                success: function (data) {
                    $("#differenceDateSpan").html(data.diffDate)
                }
            });
        }
    });

    $("#d1 :reset").click(function () {
        year1.val("");
        month1.val("");
        day1.val("");
        diffDay.val("");
    });
    $("#d2 :reset").click(function () {
        year2.val("");
        month2.val("");
        day2.val("");
        year3.val("");
        month3.val("");
        day3.val("");
    });
    $("#d3 :reset").click(function () {
        $("#d3 textarea").val("");
        codeImg.attr("src", "QRCode?content=" + textArea.val());
    });
    $("#d4 :reset").click(function () {
        $("#IDTopSix").val("");
    });

    $("#d1 :button").click(function () {
        if (diffDay.val() === "" || year1.val() === "" || month1.val() === "" || day1.val() === "" || year2.val() === "" || month2.val() === "" || day2.val() === "") {
            alert('请输入！');
        }
        $.ajax({
            type: "POST",
            url: "date/computeDate",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(
                {
                    "date1": year1.val() + "-" + month1.val() + "-" + day1.val(),
                    "differenceDate": diffDay.val()
                }
            ),
            dataType: "json",
            success: function (data) {
                $("#computeDateSpan").html(data.date);
            }
        });
    });

    $("#d2 :button").click(function () {
        if (year2.val() === "" || month2.val() === "" || day2.val() === "" || year3.val() === "" || month3.val() === "" || day3.val() === "") {
            alert('请输入！');
        }
        $.ajax({
            type: "POST",
            url: "date/differenceDate",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(
                {
                    "date2": year2.val() + "-" + month2.val() + "-" + day2.val(),
                    "date3": year3.val() + "-" + month3.val() + "-" + day3.val()
                }
            ),
            dataType: "json",
            success: function (data) {
                $("#differenceDateSpan").html(data.diffDate)
            }
        });
    });

    $("#d4 :button").click(function () {
        if ($("#IDTopSix").val() === "") {
            alert('请输入！');
        } else {
            $.ajax({
                type: "POST",
                url: "idTopSix",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(
                    {
                        "IDTopSix": $("#IDTopSix").val()
                    }
                ),
                dataType: "json",
                success: function (data) {
                    $("#IDTopSixSpan").html(data.address)
                }
            });
        }
    });

    $("#d4 :text").keyup(function (event) {
        if (event.keyCode === 13) {
            $.ajax({
                type: "POST",
                url: "idTopSix",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(
                    {
                        "IDTopSix": $("#IDTopSix").val()
                    }
                ),
                dataType: "json",
                success: function (data) {
                    $("#IDTopSixSpan").html(data.address)
                }
            });
        }
    });

    $.ajax({
        type: "POST",
        url: "GUIDGenerate",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $("#d5 span").html(data.uuid)
        }
    });
    $("#d5 :button").click(function () {
        $.ajax({
            type: "POST",
            url: "GUIDGenerate",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#d5 span").html(data.uuid)
            }
        });
    });

    $('#d6 :button:eq(0)').click(function () {
        formatJson();
    });

    $('#d6 :button:eq(1)').click(function () {
        compressJson();
    });

    $('#d7 :button').click(function () {
        $.ajax({
            type: "POST",
            url: "date/javaDateFormat",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(
                {
                    "javaTimeStamp": $("#d7 :text").val()
                }
            ),
            success: function (data) {
                // $("#d7 :text").val(data.formated);
                $("#d7 span").html(data.formated)
            }
        });
    });

    $("#d7 :text").keyup(function (event) {
        if (event.keyCode === 13) {
            $.ajax({
                type: "POST",
                url: "date/javaDateFormat",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify(
                    {
                        "javaTimeStamp": $("#d7 :text").val()
                    }
                ),
                success: function (data) {
                    $("#d7 span").html(data.formated)
                }
            });
        }
    });

    $("#d7 :text").focus(function () {
        $("#d7 :text").select()
    });

    $("#d8 span").html(md5($("#d8 :text").val()));

    $("#d8 :text").keyup(function (event) {
        let text = $("#d8 :text").val();
        $("#d8 span").html(md5(text))
    });

    $('#btnGUID').click(function () {
        toastr.info('复制成功', '提示');
        copyToClipboard($('#spanGUID').text());
    });
    $('#d8 span').click(function () {
        toastr.info('复制成功', '提示');
        copyToClipboard($('#d8 span').text())
    });

    let randomNumbers = '';
    $("#d9 :button").click(function () {
        let count = parseInt($("#d9 :text:eq(0)").val());
        let min = parseInt($("#d9 :text:eq(1)").val());
        let max = parseInt($("#d9 :text:eq(2)").val())+1;
        let splitType = $("#d9 select").val();
        switch (splitType) {
            case 'none':
                randomNumbers = '';
                for (let i = 0; i < count; i++) {
                    let num = Math.floor(Math.random()*(max-min)+min);
                    randomNumbers = randomNumbers + "" + num;
                }
                break;
            case 'whiteSpace':
                randomNumbers = '';
                for (let i = 0; i < count; i++) {
                    let num = Math.floor(Math.random()*(max-min)+min);
                    randomNumbers = randomNumbers + " " + num;
                }
                break;
            case 'comma':
                randomNumbers = '';
                for (let i = 0; i < count; i++) {
                    let num = Math.floor(Math.random()*(max-min)+min);
                    randomNumbers = randomNumbers + "," + num;
                }
                break;
            case 'dot':
                randomNumbers = '';
                for (let i = 0; i < count; i++) {
                    let num = Math.floor(Math.random()*(max-min)+min);
                    randomNumbers = randomNumbers + "." + num;
                }
                break;
            default:
                break
        }
        randomNumbers = randomNumbers.substring(1);
        $("#randomResult").html(randomNumbers);
        toastr.info('复制成功', '提示');
        copyToClipboard(randomNumbers);
        console.log(randomNumbers);
    });

    let d10Span1 = $('#d10 span:eq(1)');
    let d10Span2 = $('#d10 span:eq(2)');
    $('#d10 :button:eq(0)').click(function () {
        $('#d10 :text').val('');
        d10Span1.text('');
        d10Span2.text('');
    });
    $('#d10 :button:eq(1)').click(function () {
        let text = $('#d10 :text').val();
        d10Span1.text(d10Span1.text() + 'md5' + ' ');
        if (d10Span2.text() === '') {
            d10Span2.text(md5(text));
        } else {
            d10Span2.text(md5(d10Span2.text()))
        }
    });

    $('#d10 :button:eq(2)').click(function () {
        let text = $('#d10 :text').val();
        d10Span1.text(d10Span1.text() + 'sha1' + ' ');
        if (d10Span2.text() === '') {
            d10Span2.text(sha1(text));
        } else {
            d10Span2.text(sha1(d10Span2.text()))
        }
    });

    $('#d10 :button:eq(3)').click(function () {
        let text = $('#d10 :text').val();
        d10Span1.text(d10Span1.text() + 'sha256' + ' ');
        if (d10Span2.text() === '') {
            d10Span2.text(sha256(text));
        } else {
            d10Span2.text(sha256(d10Span2.text()))
        }
    });

    $('#d10 :button:eq(4)').click(function () {
        let text = $('#d10 :text').val();
        d10Span1.text(d10Span1.text() + 'sha512' + ' ');
        if (d10Span2.text() === '') {
            d10Span2.text(sha512(text));
        } else {
            d10Span2.text(sha512(d10Span2.text()));
        }
    });

    d10Span2.click(function () {
        copyToClipboard(d10Span2.text());
        toastr.info('复制成功', '提示');
    });


    //代码高亮插件
    var editor = CodeMirror.fromTextArea(document.getElementById("sourcejson"), {
        lineNumbers: true,
        styleActiveLine: true,
        matchBrackets: true,
        extraKeys: {
            mode:"text/html",
            "Alt-/": function (e) {
                CodeMirror.showHint(e);
            },
            "space": function (e) {
                CodeMirror.showHint(e);
            }
        }
    });

    function copyToClipboard(text) {
        let input = document.createElement('input');
        document.body.appendChild(input);
        input.setAttribute('value', text);
        input.select();
        if (document.execCommand('copy')) {
            document.execCommand('copy');
            console.log('复制成功');
        }
        document.body.removeChild(input);
    }

    function repeat(s, count) {
        return new Array(count + 1).join(s);
    }

    //json压缩
    function compressJson() {
        let jsonStr = editor.getValue();
        jsonStr = jsonStr.replace(/\s/g, '');
        editor.setValue(jsonStr)
    }

    //json格式化
    function formatJson() {
        // var json=  document.form1.sourcejson.value;
        var json = editor.getValue();

        var i = 0,
            len = 0,
            tab = "    ",
            targetJson = "",
            indentLevel = 0,
            inString = false,
            currentChar = null;


        for (i = 0, len = json.length; i < len; i += 1) {
            currentChar = json.charAt(i);

            switch (currentChar) {
                case '{':
                case '[':
                    if (!inString) {
                        targetJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
                        indentLevel += 1;
                    } else {
                        targetJson += currentChar;
                    }
                    break;
                case '}':
                case ']':
                    if (!inString) {
                        indentLevel -= 1;
                        targetJson += "\n" + repeat(tab, indentLevel) + currentChar;
                    } else {
                        targetJson += currentChar;
                    }
                    break;
                case ',':
                    if (!inString) {
                        targetJson += ",\n" + repeat(tab, indentLevel);
                    } else {
                        targetJson += currentChar;
                    }
                    break;
                case ':':
                    if (!inString) {
                        targetJson += ": ";
                    } else {
                        targetJson += currentChar;
                    }
                    break;
                case ' ':
                case "\n":
                case "\t":
                    if (inString) {
                        targetJson += currentChar;
                    }
                    break;
                case '"':
                    if (i > 0 && json.charAt(i - 1) !== '\\') {
                        inString = !inString;
                    }
                    targetJson += currentChar;
                    break;
                default:
                    targetJson += currentChar;
                    break;
            }
        }
        // document.form1.sourcejson.value=targetJson;
        editor.setValue(targetJson)
    }
});


